package com.meter.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meter.common.annotation.DataSource;
import com.meter.common.enums.DataSourceType;
import com.meter.common.handler.MyTableNameHandler;
import com.meter.common.utils.DateUtils;
import com.meter.report.domain.ReportFlow;
import com.meter.report.domain.ReportHeat;
import com.meter.report.mapper.ReportHeatMapper;
import com.meter.report.service.IReportHeatService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 热采集数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-05
 */
@Service
public class ReportHeatServiceImpl extends ServiceImpl<ReportHeatMapper, ReportHeat> implements IReportHeatService
{

    /**
     * 查询日报统计
     * @param reportHeat
     * @return
     */
    @DataSource(DataSourceType.SLAVE)
    public List<ReportHeat> selectReportHeatByDay(ReportHeat reportHeat) {
        String beginTime = reportHeat.getParams().get("beginTime").toString();
        String endTime = reportHeat.getParams().get("endTime").toString();
        String beginMonth = beginTime.isEmpty() ? DateUtils.getMonth(new Date()) : DateUtils.getMonth(beginTime);
        String endMonth = endTime.isEmpty() ? DateUtils.getMonth(new Date()) : DateUtils.getMonth(endTime);
        List<ReportHeat> list = new ArrayList<>();
        while (DateUtils.parseMonth(endMonth).getTime() >= DateUtils.parseMonth(beginMonth).getTime()) {
            MyTableNameHandler.setData(beginMonth);
            List<ReportHeat> reportHeatList = baseMapper.selectReportHeatByDay(reportHeat);
            list.addAll(reportHeatList);
            MyTableNameHandler.removeData();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtils.parseMonth(beginMonth));
            calendar.add(Calendar.MONTH, 1);
            beginMonth = DateUtils.getMonth(calendar.getTime());
        }
        return list;
    }

    /**
     * 查询月报统计
     * @param reportHeat
     * @return
     */
    @DataSource(DataSourceType.SLAVE)
    public List<ReportHeat> selectReportHeatByMonth(ReportHeat reportHeat) {
        String beginMonth = reportHeat.getParams().get("beginTime").toString().isEmpty() ? DateUtils.getMonth() : reportHeat.getParams().get("beginTime").toString();
        String endMonth = reportHeat.getParams().get("endTime").toString().isEmpty() ? DateUtils.getMonth() : reportHeat.getParams().get("endTime").toString();
        List<ReportHeat> list = new ArrayList<>();
        while(DateUtils.parseMonth(endMonth).getTime() >= DateUtils.parseDate(beginMonth).getTime()) {
            String searchMonth = beginMonth.replace("-", "");
            MyTableNameHandler.setData(searchMonth);
            List<ReportHeat> reportHeatList = baseMapper.selectReportHeatByMonth(reportHeat);
            list.addAll(reportHeatList);
            MyTableNameHandler.removeData();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtils.parseMonth(beginMonth));
            calendar.add(Calendar.MONTH, 1);
            beginMonth = DateUtils.parseDateToStr("yyyy-MM", calendar.getTime());
        }
        return list;
    }
}
